package com.zhida.smartcontrol.dialog.switchlightdialog;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;

import com.jay.tools.ui.ButtonStyle;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/16 016 16:53
 * description: 开关灯
 */
public class SwitchLightDialogFragment extends MVPBaseDialogFragment<SwitchLightDialogContract.View, SwitchLightDialogPresenter> implements SwitchLightDialogContract.View {
    @BindViews({R.id.btn_auto, R.id.btn_hand})
    List<ButtonStyle> mBtns;

    private String mType;
    private int mSwitchType;
    private List<LampSwitchBean> mLampList;

    public static SwitchLightDialogFragment newInstance(int switchType, List<LampSwitchBean> data) {
        Bundle args = new Bundle();
        args.putInt("type", switchType);
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) data);
        SwitchLightDialogFragment fragment = new SwitchLightDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_switch_light;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mSwitchType = bundle.getInt("type");
        mLampList = bundle.getParcelableArrayList("list");
    }

    @OnClick({R.id.btn_auto, R.id.btn_hand, R.id.btn_cancel, R.id.btn_confirm})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_auto:
                mBtns.get(0).setEnabled(false);
                mBtns.get(1).setEnabled(true);
                mType = "1";
                break;
            case R.id.btn_hand:
                mBtns.get(0).setEnabled(true);
                mBtns.get(1).setEnabled(false);
                mType = "0";
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                if (mSwitchType == Const.NB_SINGLE) {
                    mPresenter.openCloseNb(mLampList, mType);
                } else if (mSwitchType == Const.NB_DOUBLE) {
                    mPresenter.openCloseNbDouble(mLampList, mType);
                } else {
                    mPresenter.openClosePlcZig(mLampList, mType);
                }
                break;
        }
    }

    @Override
    public void onSuccess() {
        dismiss();
    }
}
